package com.ruoyi.business.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.dto.SeatSellSegmentDTO;
import com.ruoyi.business.domain.dto.TrainSeatGroupDTO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.DailyTrainSeat;
import com.ruoyi.business.service.IDailyTrainSeatService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 每日座位Controller
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/business/daily-train-seat")
public class DailyTrainSeatController extends BaseController
{
    @Autowired
    private IDailyTrainSeatService dailyTrainSeatService;

    /**
     * 查询每日座位列表
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:list')")
    @GetMapping("/list")
    public TableDataInfo list(DailyTrainSeat dailyTrainSeat)
    {
        startPage();
        List<DailyTrainSeat> list = dailyTrainSeatService.selectDailyTrainSeatList(dailyTrainSeat);
        return getDataTable(list);
    }

    /**
     * 导出每日座位列表
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:export')")
    @Log(title = "每日座位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DailyTrainSeat dailyTrainSeat)
    {
        List<DailyTrainSeat> list = dailyTrainSeatService.selectDailyTrainSeatList(dailyTrainSeat);
        ExcelUtil<DailyTrainSeat> util = new ExcelUtil<DailyTrainSeat>(DailyTrainSeat.class);
        util.exportExcel(response, list, "每日座位数据");
    }

    /**
     * 获取每日座位详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dailyTrainSeatService.selectDailyTrainSeatById(id));
    }

    /**
     * 新增每日座位
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:add')")
    @Log(title = "每日座位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DailyTrainSeat dailyTrainSeat)
    {
        return toAjax(dailyTrainSeatService.insertDailyTrainSeat(dailyTrainSeat));
    }

    /**
     * 修改每日座位
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:edit')")
    @Log(title = "每日座位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DailyTrainSeat dailyTrainSeat)
    {
        return toAjax(dailyTrainSeatService.updateDailyTrainSeat(dailyTrainSeat));
    }

    /**
     * 删除每日座位
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:remove')")
    @Log(title = "每日座位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dailyTrainSeatService.deleteDailyTrainSeatByIds(ids));
    }

    /**
     * 按车次和车厢分组查询座位列表
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:list')")
    @GetMapping("/listByGroup")
    public TableDataInfo listByGroup(DailyTrainSeat dailyTrainSeat) {
        List<TrainSeatGroupDTO> groups = dailyTrainSeatService.selectDailyTrainSeatGroupByTrainAndCarriage(dailyTrainSeat);

        // 手动分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        // 计算总数和当前页数据
        int total = groups.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);

        List<TrainSeatGroupDTO> pageData;
        if (fromIndex < total) {
            pageData = groups.subList(fromIndex, toIndex);
        } else {
            pageData = new ArrayList<>();
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(pageData);
        rspData.setTotal(total);
        return rspData;
    }

    @GetMapping("/sell-detail")
    public R<List<SeatSellSegmentDTO>> getSellDetail(String date, String trainCode, Long seatId) {
        List<SeatSellSegmentDTO> result = dailyTrainSeatService.getSeatSellDetail(date, trainCode, seatId);
        return R.ok(result);
    }

    /**
     * 按列车分页查询座位列表
     */
    @PreAuthorize("@ss.hasPermi('business:daily-train-seat:list')")
    @GetMapping("/listByTrainGroup")
    public TableDataInfo listByTrainGroup(DailyTrainSeat dailyTrainSeat) {
        // 获取分页信息
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        // 调用服务方法
        return dailyTrainSeatService.listByTrainPagination(dailyTrainSeat, pageNum, pageSize);
    }
}
